﻿Namespace IO
  ''' <summary>
  ''' Directory helper methods.
  ''' </summary>
  ''' <remarks></remarks>
  Public NotInheritable Class DirectoryHelper

    ''' <summary>
    ''' Returns sub-directories from a directory.
    ''' </summary>
    ''' <param name="directory">Directory to search.</param>
    ''' <returns>Colleciton of directory names.</returns>
    ''' <remarks></remarks>
    Public Shared Function Directories(ByVal directory As String) As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
      Return My.Computer.FileSystem.GetDirectories(directory)
    End Function

    ''' <summary>
    ''' Directory information.
    ''' </summary>
    ''' <param name="directory">Directory.</param>
    ''' <returns><seealso cref="DirectoryInformation"/></returns>
    ''' <remarks></remarks>
    Public Shared Function DirectoryInformation(ByVal directory As String) As DirectoryInformation
      Dim result As DirectoryInformation = Nothing

      If My.Computer.FileSystem.DirectoryExists(directory) Then
        Dim directoryInfo As System.IO.DirectoryInfo = My.Computer.FileSystem.GetDirectoryInfo(directory)

        If Not IsNothing(directoryInfo) Then
          result = New DirectoryInformation
          With result
            .Attributes = directoryInfo.Attributes
            .CreationTime = directoryInfo.CreationTime
            .FullName = directoryInfo.FullName
            .LastAccessTime = directoryInfo.LastAccessTime
            .LastWriteTime = directoryInfo.LastWriteTime
            .Name = directoryInfo.Name
            If Not directoryInfo.Parent Is Nothing Then
              .ParentDirectory = directoryInfo.Parent.FullName
            End If
            If Not directoryInfo.Root Is Nothing Then
              .Root = directoryInfo.Root.FullName
            End If
          End With
        End If

      End If

      Return result

    End Function

    ''' <summary>
    ''' Finds files in a directory.
    ''' </summary>
    ''' <param name="directory">Directory to search.</param>
    ''' <param name="searchType"><seealso cref="FileIO.SearchOption"/></param>
    ''' <returns>Collection of file names.</returns>
    ''' <remarks></remarks>
    Public Shared Function DirectoryFiles(ByVal directory As String, ByVal searchType As FileIO.SearchOption) As System.Collections.ObjectModel.ReadOnlyCollection(Of String)

      If My.Computer.FileSystem.DirectoryExists(directory) Then
        Return My.Computer.FileSystem.GetFiles(directory, searchType, Nothing)
      Else
        Return Nothing
      End If

    End Function

    ''' <summary>
    ''' Finds file information for all files in a directory.
    ''' </summary>
    ''' <param name="directory">Directory to search.</param>
    ''' <param name="searchType"><seealso cref="FileIO.SearchOption"/></param>
    ''' <returns>Collection of <seealso cref="FileInformation"/></returns>
    ''' <remarks></remarks>
    Public Shared Function DirectoryFilesInformation(ByVal directory As String, ByVal searchType As FileIO.SearchOption) As System.Collections.ObjectModel.ReadOnlyCollection(Of FileInformation)
      Dim files As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = DirectoryFiles(directory, searchType)

      Dim result As New System.Collections.Generic.List(Of FileInformation)

      If Not IsNothing(files) Then
        For Each file As String In files
          result.Add(FileHelper.FileInformation(file.Trim))
        Next
      End If

      Return New System.Collections.ObjectModel.ReadOnlyCollection(Of FileInformation)(result)

    End Function

    Private Sub New()
    End Sub

  End Class
End Namespace